package com.easy.admin.system.comnon;

import cn.hutool.Hutool;
import cn.hutool.core.swing.clipboard.ClipboardUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.PinyinUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.system.SystemUtil;
import com.alibaba.fastjson.JSON;
import com.gargoylesoftware.htmlunit.AjaxController;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class Util {
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if (ip.indexOf(",") != -1) {
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
            System.out.println("Proxy-Client-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
            System.out.println("WL-Proxy-Client-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
            System.out.println("HTTP_CLIENT_IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            System.out.println("HTTP_X_FORWARDED_FOR ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
            System.out.println("X-Real-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            System.out.println("getRemoteAddr ip: " + ip);
        }
        return ip;
    }


    /**
     * 爬虫抓取http://www.333ttt.com/up/ 上所有的音乐播放链接
     */

    public static List<String> getMusicHtmlUrl(String url) throws Exception {
        HttpClient client = new HttpClient(url);
        client.setHttps(false);
        client.get();
        String html = client.getContent();
        Document document = Jsoup.parse(html);
        Elements el = document.getElementsByClass("linbj");
        int size = el.size();
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < size; i++) {
            Element ell = el.get(i);
            Elements els = ell.select("td");
            Element element = els.get(1);
            Elements href = element.getElementsByAttribute("href");
            String musicName = href.text();
            String musicUrl = href.attr("href");
            //以上得到的链接是相对路径  加上域名 http://www.333ttt.com
            String openUrl = "http://www.333ttt.com" + musicUrl;
            //根据以上的openUrl打开页面详情，抓取具体的url链接
            HttpClient client1 = new HttpClient(openUrl);
            client1.get();
            client1.setHttps(false);
            String openHtml = client1.getContent();
            Element opels = Jsoup.parse(openHtml);
            Elements openels = opels.getElementsByClass("kuad");
            String realUrl = openels.text();
            // System.out.println(musicName+"    "+realUrl);
            map.put(musicName, realUrl);
        }
        System.out.println(JSON.toJSON(map));

        return null;
    }

    /**
     * 模拟浏览器
     *
     * @param args
     * @throws Exception
     */
    public static void useJavaChorm() {
        try {
            WebClient webClient = new WebClient(BrowserVersion.CHROME);//设置浏览器内核为谷歌
            webClient.getOptions().setJavaScriptEnabled(true);//开启js支持
            webClient.getOptions().setCssEnabled(false);//不显示页面设置成false
            webClient.getOptions().setThrowExceptionOnScriptError(false);//js抛出异常
            webClient.getOptions().setActiveXNative(false);

            //ajax
            webClient.setAjaxController(new NicelyResynchronizingAjaxController());
            webClient.getOptions().setUseInsecureSSL(false);
            webClient.getOptions().setRedirectEnabled(true);//允许重定向

            webClient.getOptions().setTimeout(5000);//设置超时时间
            webClient.setJavaScriptTimeout(3000);//js超时时间
            webClient.getCookieManager().setCookiesEnabled(true);//开启cookie
            HtmlPage page = webClient.getPage("https://www.baidu.com");
            webClient.waitForBackgroundJavaScript(5000);

            //接下来进行页面分析进行页面上的表单操作
            HtmlPage newPage = pageReady(webClient, page);
            System.out.println(newPage.asXml());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static HtmlPage pageReady(WebClient webClient, HtmlPage page) throws Exception {
        HtmlForm form = page.getHtmlElementById("form");
        HtmlTextInput input = form.getInputByName("wd");
        input.setValueAttribute("java");

      /* DomElement wl =  page.getElementById("mobile");
        wl.setAttribute("value","17693151456");
        DomElement wp =  page.getElementById("mobile");
        wp.setAttribute("userPassword","123");*/
        DomElement buttion = page.getElementById("loginBtn");
        HtmlPage newpage = buttion.click();

        return newpage;
    }


    public static void main(String[] args) throws Exception {
        //  getMusicHtmlUrl("http://www.333ttt.com/up/?u=&page=4&id=");
        //System.out.println(RuntimeUtil.execForStr("ipconfig"));
        System.out.println(ClipboardUtil.getStr());
        ClipboardUtil.setStr("121212");
        //useJavaChorm();
     /* String py =   PinyinUtil.getPinYin("王勃");
      System.out.println(py);
        String pro = IdcardUtil.getProvinceByIdCard("63311019961109131X");
        System.out.println(pro);*/
        System.out.print(IdcardUtil.getAgeByIdCard("62282219961109131X"));

    }
}
